<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['columnDisplay', 'quickSearch']"
            :quick-search-placeholder="t('搜索 订单号/用户名')"
        >
            <template #btnBefore>
                <span style="" class="btn btn-success ml-12" @click="router.push({ path: '/user/order/order/place_order' })">去下单</span>
            </template>
        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef">

            <template #order_number>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column prop="id" label="订单编号" align="center">
                    <template #default="scope">
                        <div class="order-number" @click="detail(scope.row.out_trade_no)">
                            {{ scope.row.out_trade_no }}
                        </div>
                    </template>
                </el-table-column>
            </template>

        </Table>

        <!-- 表单 -->
        <PopupForm />

        
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

import router from '/@/router'

import { confirmOrder, cancelOrder } from '/@/api/frontend/order/order';

defineOptions({
    name: 'order/order',
})

const { t } = useI18n()
const tableRef = ref()

let optButtons = defaultOptButtons([])

// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'download', // 按钮名称
        title: 'download', // 鼠标放置时的 title 提示
        class: 'btn margin-left-none btn-info',
        text: '确认', // 直接在按钮内显示的文字，title 有值时可为空
        type: '', // 按钮类型，请参考 element plus 的按钮类型
        icon: '', // 按钮 icon
        disabledTip: true, // tipButton 禁用 tip
        click: (row: TableRow, field: TableColumn) => { // 自定义点击事件
            confirmOrder({order_id: row.id}).then((e) => {
                console.log(e)
                if(e.code == 1){
                    row.status = 1;
                    row.status_text = '已确认'
                }
            })
        },
        display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
            return row.status == 1 ? false : true;
        },
        disabled: (row: TableRow, field: TableColumn) => { // 按钮是否禁用，请返回布尔值
            // return row.file ? false : true;
        },
        attr: {} // 自定义el-button属性
    },{
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'download', // 按钮名称
        title: 'download', // 鼠标放置时的 title 提示
        class: 'btn margin-left-none btn-danger',
        text: '取消', // 直接在按钮内显示的文字，title 有值时可为空
        type: '', // 按钮类型，请参考 element plus 的按钮类型
        icon: '', // 按钮 icon
        disabledTip: true, // tipButton 禁用 tip
        click: (row: TableRow, field: TableColumn) => { // 自定义点击事件
            cancelOrder({order_id: row.id}).then((e) => {
                if(e.code == 1){
                    row.status = 0;
                    row.status_text = '未确认'
                }
            })
        },
        display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
            return row.status == 0 ? false : true;
        },
        disabled: (row: TableRow, field: TableColumn) => { // 按钮是否禁用，请返回布尔值
            // return row.file ? false : true;
        },
        attr: {} // 自定义el-button属性
    },{
        render: 'confirmButton',
        name: 'delete',
        title: '',
        text: '删除',
        type: 'danger',
        class: 'btn btn-danger margin-left-none',
        icon: '',
        popconfirm: {
            confirmButtonText: t('Delete'),
            cancelButtonText: t('Cancel'),
            confirmButtonType: 'danger',
            title: t('Are you sure to delete the selected record?'),
        },
        display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
            return row.status == 1 ? false : true;
        },
    }
]

// 新按钮合入到默认的按钮数组
optButtons = newButton.concat(optButtons)

const state = reactive({
    live_id: 0,

})

/**
 * 关闭订单详情
 */
const hideDetail = () => {
    state.live_id = 0;
}

/**
 * 打开订单详情
 */
const detail = (live_id: number) => {
    window.open('/user/order/order/detail/' + live_id)
}


/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/user/order.Order/'),
    {
        pk: 'id',
        column: [
            // { type: 'selection', align: 'center', operator: false },
            { 
                label: t('order.order.out_trade_no'), 
                prop: 'out_trade_no', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE', 
                sortable: false,
                // width: 180
                render: 'slot',
                slotName: 'order_number'

            },
            { 
                label: t('客户名'), 
                prop: 'user.username', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE'
            },
            { 
                label: t('下单时间'), 
                prop: 'place_order_time', 
                align: 'center', 
                render: 'datetime', 
                operator: 'RANGE', 
                sortable: 'custom', 
                width: 160, 
                timeFormat: 'mm-dd/hh:MM'
            },
            { 
                label: t('订单总价'), 
                prop: 'total_price', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE'
            },
            { 
                label: t('订单状态'), 
                prop: 'status_text', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE'
            },
            { label: t('Operate'), align: 'center', width: 180, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: ['all'],
    },
    {
        defaultItems: { user_id: null, out_trade_no: null, goods_image: null, goods_name: null, color: null, size: null, number: 0, purchase_price: 0, total_price: 0, type: null, note: null, draft: 1, shipment_status: 0, place_order_time: 0, shipment_status_update_time: 0 },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.table.filter!.type = 1;
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">
:deep(.table-header > .ml-12:first-child){
    margin-left: 0;
}
.order-number{
    cursor: pointer;
}
.order-number:hover{
    text-decoration: underline;
}
:deep(.ba-data-table .margin-left-none){
    margin-left: 0!important;
    margin-bottom: 0px;
    padding: 4px 14px;
}
:deep(.ba-data-table .table-operate-text){
    padding-left: 0;
}
:deep(.font-size-13){
    font-size: 13px;
}
</style>
